<?php

/*	Vulpes - Web-based managegent for wireless internet providers.
	Copyright (C) 2007 Alwin Garside

	This file is part of Vulpes.

	Vulpes is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>. */

require_once 'controller.php';

class tools_ctl extends controller
{
	protected $db;
	
	function __construct()
	{
		parent::__construct();
		$this->db = db::get_instance();
	}
	
	function arp()
	{
		$sql_clients = '
			SELECT `clients`.`username`, `contracts`.`ip`
			FROM `clients`
			INNER JOIN `contracts` ON (
				`clients`.`client_id` = `contracts`.`client_id` )';
		if(!$result_clients = $this->db->query($sql_clients)) break;
		$clients = array();
		while ($client = $result_clients->fetchObject()) {
			$clients[strtoupper($client->mac)] = $client->username;
		}
	
		$arp = arp();

		$result .= '<arp>';
		foreach ($arp as $machine) {
			$result .= '<machine>';
			$result .= '<username>'.$clients[$machine['hw_address']].'</username>';
			$result .= xml_iterate($machine);
			$result .= '</machine>';
		}
		$result .= '</arp>';

		return $result;
	}

	function ping()
	{
		$output = array();
		exec('ping -W 5 -c 1 '.get('host'), $output);
		$result .= '
			<ping>
				<host>'.get('host').'</host>
				<ping1>'.$output[1].'</ping1>
				<ping2>'.$output[3].'</ping2>
			</ping>';

		return $result;
	}

	function squid()
	{
		$output = array();
		exec('tail -n50 /var/log/squid/access.log', $output);
		$result .= '<squid>';
		foreach ($output as $value) {
			$result .= '<line>'.$value.'</line>';
		}
		$result .= '</squid>';

		return $result;
	}
}

?>